﻿using ProjectSemIII.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Survey
{
    public partial class ViewResult : System.Web.UI.Page
    {
        private Entity.Survey sv;
        private SurveyProcess surveyProcess;
        private int surveyID;
        private int count;
        private List<Entity.Question> listQuestion;
        private List<Entity.Result> listResult;
        private Entity.Question question;
        private List<Entity.Answer> listAnswers;
        private const bool blnAllowPaging = true;
        private const int iPageSize = 5;
        private static PagedDataSource pgdProducts;
        protected void Page_Load(object sender, EventArgs e)
        {
            surveyProcess = new SurveyProcess();
            if (Request.QueryString.Count == 2)
            {
                if (Request.QueryString.Keys[0].ToString() == "surveyid" && Request.QueryString.Keys[1].ToString() == "count")
                {
                    surveyID = Convert.ToInt32(Request.QueryString["surveyid"].ToString());
                    listQuestion = surveyProcess.getAllQuestionBySurveyID(surveyID);
                    count = Convert.ToInt32(Request.QueryString["count"].ToString());
                    question = surveyProcess.getQuestionByID(listQuestion[count].QuestionID);
                    pgdProducts = new PagedDataSource();
                    if (!Page.IsPostBack)
                    {
                        this.getSurveyInfo();
                        this.getSurveyQuestionResult();
                    }
                }
                else
                {
                    Response.Redirect("/404.html");
                }
            }
            else
            {
                Response.Redirect("/404.html");
            }
        }

        private void getSurveyInfo()
        {
            sv = surveyProcess.getSurveyByID(surveyID);
            lbSurveyName.Text = sv.SurveyName;
            lbDesc.Text = sv.SurveyDesc;
            imageSurvey.ImageUrl = "~/Images/Surveys/" + sv.SurveyImage;
            lbStartDate.Text = sv.SurveyStartDate.ToShortDateString();
            lbDeadLine.Text = sv.SurveyDateLine.ToShortDateString();
            if (sv.SurveyPlayer == 1)
            {
                lbJoinGroup.Text = "Student";
            }
            else
            {
                lbJoinGroup.Text = "Staff";
            }
            lbCatalog.Text = surveyProcess.getSurveyCatalogName(sv.CatalogID);
        }

        private void getSurveyQuestionResult()
        {
            questionNumber.InnerText = "Question Number " + (count + 1);
            if (count == 0)
            {
                btPrev.Visible = false;
            }
            else
            {
                btPrev.Visible = true;
            }

            if (count + 1 == listQuestion.Count)
            {
                btNext.Visible = false;
            }
            else
            {
                btNext.Visible = true;
            }
            lbQuestionDetail.Text = question.QuestionDetail;
            if (question.QuestionTypeID == 2)
            {
                nonComment.Visible = false;
                pagination.Visible = true;
                listAnswers = surveyProcess.getAnswersCommentByQuestionID(listQuestion[count].QuestionID);

                pgdProducts.AllowPaging = blnAllowPaging;
                pgdProducts.PageSize = iPageSize;
                pgdProducts.DataSource = listAnswers;

                pgdProducts.CurrentPageIndex = ThisPageNumber;
                fillTotalPage(pgdProducts.PageCount);

                resultComment.DataSource = pgdProducts;
                resultComment.DataBind();
            }
            else
            {
                nonComment.Visible = true;
                pagination.Visible = false;
                listResult = surveyProcess.getResultListFromQuestionID(listQuestion[count].QuestionID);
                allResult.DataSource = listResult;
                allResult.DataBind();
                if (sv.CatalogID == 1)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        int result = surveyProcess.updateWinner(sv.CatalogID, listResult[i].count, listResult[i].title);
                        if (result == 10)
                        {

                        }
                        else
                        {

                        }
                    }
                }
            }
        }

        private void fillTotalPage(int iTotalPages)
        {
            List<int> pageID = new List<int>();
            for (int i = 1; i <= iTotalPages; i++)
            {
                pageID.Add(i);
            }
            totalPage.DataSource = pageID;
            totalPage.DataBind();
        }

        private int ThisPageNumber
        {
            get
            {
                object obj = ViewState["ThisPageNumber"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                this.ViewState["ThisPageNumber"] = value;
            }
        }

        protected void btPrev_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewResult.aspx?surveyid=" + surveyID + "&count=" + (count - 1));
        }

        protected void btNext_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewResult.aspx?surveyid=" + surveyID + "&count=" + (count + 1));
        }

        protected void PageNumber_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            ThisPageNumber = Convert.ToInt32(lbuton.Text) - 1;
            this.getSurveyQuestionResult();
        }

        protected void btPrevPage_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            if (lbuton.ID == "btPrevPage")
            {
                if (ThisPageNumber > 0)
                {
                    ThisPageNumber = ThisPageNumber - 1;
                    this.getSurveyQuestionResult();
                }
            }
            //Change page when user click to next number button
            else if (lbuton.ID == "btNextPage")
            {
                if (ThisPageNumber < totalPage.Items.Count - 1)
                {
                    ThisPageNumber = ThisPageNumber + 1;
                    this.getSurveyQuestionResult();
                }
            }
        }
    }
}